<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BDb
 *
 * @author jianfeng
 */
class BSpaceEngineDb extends BDbAdapter {

    public function getSpaceType($spaceId) {
        $sql = "select typename from bln_spacetype t,bln_space_Spacetype s
             where t._id = s.Spacetype_id and s.space_id = :spaceId";

        $db = Yii::app()->getDb();

        $command = $db->createCommand($sql);

        $command->bindParam(":spaceId", $spaceId, PDO::PARAM_INT);

        $typename = $command->queryScalar();

        return $typename;
    }

    public function getSpaceCategory($spaceId) {
        $sql = "select categoryname from bln_spacecategory c,bln_space_Spacecategory s
             where c._id = s.Spacecategory_id and s.space_id = :spaceId";

        $db = Yii::app()->getDb();

        $command = $db->createCommand($sql);

        $command->bindParam(":spaceId", $spaceId, PDO::PARAM_INT);

        $categoryname = $command->queryScalar();

        return $categoryname;
    }

    public function getApps($spaceId) {
        $apps = array();

        $sql = "select * from bln_space_application where space_id = :spaceId";

        $db = Yii::app()->getDb();

        $command = $db->createCommand($sql);

        $command->bindParam(":spaceId", $spaceId, PDO::PARAM_INT);

        return $dataReader = $command->queryAll();

        foreach ($dataReader as $row) {
           // $appClass = $this->getAppClass($row['app_id']);
            // $app = new $appClass;
            // $app->load($row['app_id']);
            //$apps[$row['app_id']] = new $appClass;
            array_push($apps, array('appId'=>$row['app_id']));           
        }

        return $apps;
    }

    public function setSpaceVisible($spaceId, $visible = TRUE) {
        $sql1 = "select visible from bln_space_visible where space_id = :spaceId";
        $sql2 = "insert bln_space_visible(space_id,visible)values(:spaceId,:Visible)";
        $sql3 = "update bln_space_visible set visible=:Visible where space_id = :spaceId";

        if ($visible != FALSE)
            $visible = TRUE;

        $db = Yii::app()->getDb();

        $command1 = $db->createCommand($sql1);
        $command1->bindParam(":spaceId", $spaceId, PDO::PARAM_INT);

        $command2 = $db->createCommand($sql2);
        $command2->bindParam(":spaceId", $spaceId, PDO::PARAM_INT);
        $command2->bindParam(":Visible", $visible, PDO::PARAM_BOOL);

        $command3 = $db->createCommand($sql3);
        $command3->bindParam(":spaceId", $spaceId, PDO::PARAM_INT);
        $command3->bindParam(":Visible", $visible, PDO::PARAM_BOOL);

        if ($spaceId != NULL) {
            if (!$command1->queryRow()) {
                //insert
                $command2->execute();
            } else {
                //update
                $command3->execute();
            }
        }
    }

    public function getSpaceVisible($spaceId) {
        $sql1 = "select visible from bln_space_visible where space_id = :spaceId";

        $db = Yii::app()->getDb();

        $command1 = $db->createCommand($sql1);
        $command1->bindParam(":spaceId", $spaceId, PDO::PARAM_INT);

        if (!$command1->queryRow())
            return TRUE;

        return $command1->queryScalar();
    }
    
    public function addSpaceResourceNode($data){
        
    }
    
    public function addSpaceResourceItem($data){
        
    }
    
    public function getSpaceResourceNodes($data){
        
    }
    
    public function getSpaceResourceItems($data){
        
    }
    
    public function getSpaceResourceNodeChildren($data){
        
    }

}

?>